package cn.xyfmzmy.adapter.spring.etcd;


import io.etcd.jetcd.ByteSequence;
import io.etcd.jetcd.Client;
import io.etcd.jetcd.kv.GetResponse;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;

public class EtcdDemo {

    public static void main(String[] args) throws Exception{
        Client client = Client.builder().endpoints("http://127.0.0.1:2379").build();
        ByteSequence etcdKey = ByteSequence.from("xyf-key", StandardCharsets.UTF_8);
        kvGet(client,etcdKey);
        ByteSequence value = ByteSequence.from("xyf-value", StandardCharsets.UTF_8);
        client.getKVClient().put(etcdKey,value).get();
        kvGet(client,etcdKey);

    }

    private static void kvGet(Client client, ByteSequence etcdKey) throws Exception{
        CompletableFuture<GetResponse> getResponseCompletableFuture = client.getKVClient().get(etcdKey);
        GetResponse getResponse = getResponseCompletableFuture.get();
        if(getResponse.getCount() > 0){
            System.out.println(getResponse.getKvs().get(0).getValue().toString(StandardCharsets.UTF_8));
        }else{
            System.out.println("key 没有对应的值");
        }
    }

}
